﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Daneas.Utility.Universals
{

    public class JSHelper
    {
        /// <summary>
        /// 警报
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message"></param>
        /// <param name="reflash"></param>
        public static void Alert(Page page, string message, bool reflash)
        {
            string js;

            if (reflash)
            {
                js = string.Format("<script language=\"javascript\" type=\"text/javascript\">alert(\"{0}\");window.location.href=window.location.href;</script>", message);
            }
            else
            {
                js = string.Format("<script language=\"javascript\" type=\"text/javascript\">alert(\"{0}\")</script>", message);
            }
            page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), message.GetHashCode().ToString(), js);
        }

        public static void AlertAndToPage(Page page, string msg, string url)
        {
            string js = "";

            js = string.Format("<script language=\"javascript\" type=\"text/javascript\">alert(\"{0}\");window.location.href=\"{1}\";</script>", msg, url);

            page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), msg.GetHashCode().ToString(), js);
        }

        public static void ToPage(Page page, string url)
        {
            string js = "<script language=\"javascript\" type=\"text/javascript\">window.location.href='{0}';</script>";
            js = string.Format(js, url);
            page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), url.GetHashCode().ToString(), js);
        }

        public static void OpenPage(Page page, string url)
        {
            string js = "<script language=\"javascript\" type=\"text/javascript\">window.open({0});</script>";
            js = string.Format(js, url);
            page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), url.GetHashCode().ToString(), js);
        }

        public static void JsInit(Page page, string handler)
        {
            string js = "<script language=\"javascript\" type=\"text/javascript\">" + handler + "</script>";
            page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), handler.GetHashCode().ToString(), js);
        }


        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(System.Web.UI.Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');window.location=\"" + url + "\"</script>");

        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirects(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());

        }

        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript'>" + script + "</script>");

        }
    }
}
